var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    current_page: 0,
    isLoadedAll: false,
    employeeData: [],
    index: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
    this.setData({
      type: type
    })
    this.getEmployeeData();
  },
  onShow: function () {
    if (this.data.index != 0) {
      this.data.employeeData = [];
      this.getEmployeeData();
    }
    this.data.index++;
  },
  /**
   * 获取员工列表
   */
  getEmployeeData: function () {
    var that = this;
    var storeId = app.globalData.storeId;
    var data = {
      page: this.data.current_page
    };
    req.storeRequest("请稍等", "POST", url.store['getStoreUserList'], storeId, data, function back(res) {
      var data = res.data;
      console.log(res.data);

      if (that.data.employeeData.length == 0 && data.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '暂无记录',
        })
      }
      if (data.length != 0) {
        that.data.employeeData.push.apply(that.data.employeeData, data);
        that.setData({
          employeeData: that.data.employeeData
        })
        console.log(that.data.current_page)
      } else {
        wx.showToast({
          icon: 'none',
          title: '已经到底了！',
        })
        that.data.isLoadedAll = true; //全部加载完毕
        that.data.current_page = 0; //当前页面重新设置成1
      }
    });
  },
  /**
   * 删除员工
   */
  del: function (event) {
    var that = this;
    var storeId = app.globalData.storeId;
    var index = event.currentTarget.dataset.index;
    var userId = event.currentTarget.dataset.id;
    var data = {
      storeUserId: userId
    }
    wx.showModal({
      title: '提示',
      content: '您确定删除这个员工',
      success: function (res) {
        if (res.confirm) {
          req.storeRequest("请稍等", "POST", url.store["deleteStaff"], storeId, data, function back(res) {
            var employeeData = that.data.employeeData;
            employeeData.splice(index, 1);
            that.setData({
              employeeData: employeeData
            })
          })
          console.log('删除')
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isLoadedAll) {
      this.data.current_page++;
      this.getEmployeeData();
    }
  },
  /**
   * 下拉刷新用户收藏的商品数据
   */
  onPullDownRefresh: function () {
    if (!this.data.isLoadedAll) {
      this.data.current_page++;
      this.getConsumption();
    }
    wx.stopPullDownRefresh();
  },
  /**
   * 跳转到员工操作
   */
  gotoDetail: function (e) {
    var userId = e.currentTarget.dataset.id;
    var type = this.data.type;
    wx.navigateTo({
      url: '../employeeInfo/employeeInfo?type=' + type + "&userId=" + userId,
    })
  }
})